//
//  ChangePwdViewController.m
//  liangyi
//
//  Created by 张水 on 17/1/5.
//  Copyright © 2017年 yswh. All rights reserved.
//

#import "ChangePasswordViewController.h"

@interface ChangePasswordViewController ()

@property(nonatomic,strong) CustomTextField *userPhone;

@property(nonatomic,strong) CustomTextField *userVerity;

@property(nonatomic,strong) CustomTextField *userOldPassWord;

@property(nonatomic,strong) CustomTextField *userNewPwd;

@property(nonatomic,strong) CustomTextField *userReNewPwd;

@property(nonatomic,strong) UIButton        *btnVerity;

@property (strong, nonatomic) UIButton      *btnRegister;

@property (strong, nonatomic) NSTimer       *timer;

@property (assign, nonatomic) NSInteger     index;

@property (nonatomic, strong) NSString      *strPhone;

@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    [self setupData];
    [self setupUI];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.timer invalidate];
}

-(void)setupData {
    self.index = 60;
}

-(void)setupUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.userPhone = [[CustomTextField alloc]init];
    [self.userPhone setLeftTitleFont:[UIFont systemFontOfSize:14]];
    self.userPhone.leftTitle = @"手机号码";
    self.userPhone.keyboardType =  UIKeyboardTypeNumberPad;
    self.userPhone.leftTitleColor = [UIColor ColorWithHex:@"#7d7d7d"];
    self.userPhone.placeholder = @"输入手机号码";
    self.userPhone.validator = [PhoneValidator new];
    [self.userPhone setValue:[UIColor ColorWithHex:@"#a5a5a5"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.userPhone setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.userPhone.layer.cornerRadius = 23;
    self.userPhone.layer.borderColor = [UIColor ColorWithHex:@"#efefef"].CGColor;
    self.userPhone.layer.borderWidth = 2;
    [self.view addSubview:self.userPhone];
    [self.userPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(90);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    
    UILabel *backLable =[[UILabel alloc]init];
    [self.view addSubview:backLable];
    backLable.backgroundColor = [UIColor ColorWithHex:@"#5ac3d1"];
    [backLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.userPhone.mas_height);
        make.width.equalTo(@70);
        make.centerY.equalTo(self.userPhone.mas_centerY);
        make.right.equalTo(self.userPhone.mas_right).offset(-30);
    }];
    
    
    self.btnVerity = [UIButton new];
    [self.btnVerity setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.btnVerity.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.btnVerity setTitleColor:[UIColor ColorWithHex:@"#025d69"] forState:UIControlStateNormal];
    [self.btnVerity.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.btnVerity setBackgroundColor:[UIColor ColorWithHex:@"#5ac3d1"]];
    self.btnVerity.layer.cornerRadius = 23;
    [self.btnVerity addTarget:self action:@selector(sendVerityCode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btnVerity];
    [self.btnVerity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.userPhone.mas_height);
        make.width.equalTo(@100);
        make.centerY.equalTo(self.userPhone.mas_centerY);
        make.right.equalTo(self.userPhone.mas_right);
    }];
    
    
    
    
    self.userVerity = [[CustomTextField alloc]init];
    [self.userVerity setLeftTitleFont:[UIFont systemFontOfSize:14]];
    self.userVerity.leftTitle = @"验证码";
    self.userVerity.keyboardType =  UIKeyboardTypeNumberPad;
    self.userVerity.leftTitleColor = [UIColor ColorWithHex:@"#7d7d7d"];
    self.userVerity.placeholder = @"输入验证码";
    [self.userVerity setValue:[UIColor ColorWithHex:@"#a5a5a5"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.userVerity setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.userVerity.layer.cornerRadius = 23;
    self.userVerity.layer.borderColor = [UIColor ColorWithHex:@"#efefef"].CGColor;
    self.userVerity.layer.borderWidth = 2;
    [self.view addSubview:self.userVerity];
    [self.userVerity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userPhone.mas_bottom).offset(25);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    
    self.userOldPassWord = [[CustomTextField alloc]init];
    [self.userOldPassWord setLeftTitleFont:[UIFont systemFontOfSize:14]];
    self.userOldPassWord.leftTitle = @"原密码";
    self.userOldPassWord.leftTitleColor = [UIColor ColorWithHex:@"#7d7d7d"];
    self.userOldPassWord.placeholder = @"请输入原密码";
    [self.userOldPassWord setValue:[UIColor ColorWithHex:@"#a5a5a5"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.userOldPassWord setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.userOldPassWord.layer.cornerRadius = 23;
    self.userOldPassWord.layer.borderColor = [UIColor ColorWithHex:@"#efefef"].CGColor;
    self.userOldPassWord.layer.borderWidth = 2;
    [self.view addSubview:self.userOldPassWord];
    [self.userOldPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userVerity.mas_bottom).offset(25);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    
    
    self.userNewPwd = [[CustomTextField alloc]init];
    [self.userNewPwd setLeftTitleFont:[UIFont systemFontOfSize:14]];
    self.userNewPwd.secureTextEntry = YES;
    self.userNewPwd.leftTitle = @"新密码";
    self.userNewPwd.leftTitleColor = [UIColor ColorWithHex:@"#7d7d7d"];
    self.userNewPwd.placeholder = @"请输入新密码";
    [self.userNewPwd setValue:[UIColor ColorWithHex:@"#a5a5a5"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.userNewPwd setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.userNewPwd.layer.cornerRadius = 23;
    self.userNewPwd.layer.borderColor = [UIColor ColorWithHex:@"#efefef"].CGColor;
    self.userNewPwd.layer.borderWidth = 2;
    [self.view addSubview:self.userNewPwd];
    [self.userNewPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userOldPassWord.mas_bottom).offset(25);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    
    self.userReNewPwd = [[CustomTextField alloc]init];
    [self.userReNewPwd setLeftTitleFont:[UIFont systemFontOfSize:14]];
    self.userReNewPwd.secureTextEntry = YES;
    self.userReNewPwd.leftTitle = @"重复密码";
    self.userReNewPwd.leftTitleColor = [UIColor ColorWithHex:@"#7d7d7d"];
    self.userReNewPwd.placeholder = @"重复新密码";
    [self.userReNewPwd setValue:[UIColor ColorWithHex:@"#a5a5a5"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.userReNewPwd setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.userReNewPwd.layer.cornerRadius = 23;
    self.userReNewPwd.layer.borderColor = [UIColor ColorWithHex:@"#efefef"].CGColor;
    self.userReNewPwd.layer.borderWidth = 2;
    [self.view addSubview:self.userReNewPwd];
    [self.userReNewPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userNewPwd.mas_bottom).offset(25);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    
    
    self.btnRegister = [[UIButton alloc]init];
    self.btnRegister.backgroundColor = [UIColor ColorWithHex:@"#5ac3d1"];
    self.btnRegister.layer.cornerRadius = 23;
    [self.btnRegister setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btnRegister setTitle:@"确定" forState:UIControlStateNormal];
    [self.btnRegister addTarget:self action:@selector(updateUserPwd) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btnRegister];
    [self.btnRegister mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userReNewPwd.mas_bottom).offset(50);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@48);
    }];
    
    
}



-(void)sendVerityCode{
    
    [self getTimeDidClick];
    
    NSString *url = [BASEURL stringByAppendingString:kURL_Sendsend];
    
    NSDictionary *dict= @{@"phone":self.userPhone.text,
                          @"position":@"updatepwd"};
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString};
    
    [AppNetwork requestWithPath:url parameters:parameters success:^(id responseObject) {
        
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        NSLog(@"%@", responseObject);
        NSLog(@"验证码");
        if (code == 0) {
            
        }else{
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
        
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:@"连接失败,请检查您的网络"];
    }];
    
}

-(void)getTimeDidClick{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0f * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(timer, ^{
        if (self.index <= 0 ){
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.index = 60;
                self.btnVerity.userInteractionEnabled = YES;
                [self.btnVerity setTitle:@"获取验证码" forState:UIControlStateNormal];
            });
        }else{
            self.index--;
            self.btnVerity.userInteractionEnabled = NO;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.btnVerity setTitle:[NSString stringWithFormat:@"重新发送(%ld)",(long)self.index] forState:UIControlStateNormal];
            });
        }
    });
    dispatch_resume(timer);
}
-(void)updateUserPwd{
    
    NSString *url = [BASEURL stringByAppendingString:kURL_UpdatePwd];
    NSDictionary *dict = @{@"phone":self.userPhone.text,
                           @"phoneCode":self.userVerity.text,
                           @"userPass":self.userOldPassWord.text,
                           @"userPassNew":self.userNewPwd.text,
                           @"userPassReNew":self.userReNewPwd.text};
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSLog(@"%@",jsonString);
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString,
                                 @"login":[NSString  AES128Encrypt:[NSString stringWithFormat:@"%@@@%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"userid"],ApplicationDelegate.deviceID]]};
    [AppNetwork requestWithPath:url parameters:parameters success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
            [[[UIAlertView alloc] initWithTitle:@"修改密码成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil] show];
        }else{
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        NSLog(@"%@",error);
        [CustomProgressHUD showErrorWithStatus:@"连接失败,请检查您的网络"];
    }];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
